<?
class AdminPromotion extends Module  
{
	function AdminPromotion($row)
	{	
		Module::Module($row);
		
		if(User::have_permit(ADMIN_ADV))
		{
			switch (Url::get('cmd'))
			{
				case 'edit':
					$promo	=	urldecode(Url::get('name'));
					
					if($promo && is_dir(ROOT_PATH.'promotion/'.$promo.'/'))
					{
						$this->add_form(new EditPromotionForm());
					}
					else
					{
						Url::redirect_current();
					}
					break;
				case 'delete':
					$promo	=urldecode(Url::get('name'));
					
					if($promo && is_dir(ROOT_PATH.'promotion/'.$promo.'/'))
					{
						AZLib::delDir(ROOT_PATH.'promotion/'.$promo.'/');
					}
					
					Url::redirect_current();
					break;
				default:
					$this->add_form(new AdminPromotionForm());	
					break;
			}
		}
		else 
			Url::access_denied();
	}
}

class  AdminPromotionForm extends Form 
{
	function __construct()
	{
		Form::__construct('AdminPromotionForm');
	}
	
	function on_submit()
	{
		if (isset($_FILES['zipfile']) && $_FILES['zipfile']['error']==0)
		{	
			//$fileEXT=strtolower(substr($_FILES['zipfile']['name'],strlen($_FILES['zipfile']['name'])-4,4));
			$fileEXT	= AZLib::getExtension($_FILES['zipfile']['name']);
			
			if($fileEXT=='.zip')
			{
				$uploadPath = ROOT_PATH.'promotion/';
				
				@chmod($uploadPath,0777);
				
				$zipFilePath = $uploadPath.time().$fileEXT;
				@chmod($zipFilePath,0777);
				
				if(!move_uploaded_file( $_FILES['zipfile']['tmp_name'], $zipFilePath))
				{
					$this->setFormError('zipfile',"Không Upload được file Zip !");
					return ;
				}
				else 
				{
					@chmod($zipFilePath,0777);
					require_once "includes/unzip.ncl.php";
					
					if (file_exists($zipFilePath)) 
					{
						$txt='';
						//unzipping...
						$zip = new PclZip($zipFilePath);
						if (($list = $zip->listContent()) == 0) 
						{
							$this->setFormError('zipfile',"File Zip trống!");
							@unlink($zipFilePath);
							return ;
						}
						
						//calculate statistics...
						for ($i=0; $i<sizeof($list); $i++) 
						{
						    if ($list[$i]['folder']=='1') 
						    {
						    	$fold++;
						       	$dirs[$fold] 	= $list[$i]['stored_filename'];
								$dirname 		= $list[$i]['stored_filename'];
								$dirname 		= substr($dirname,0,strlen($dirname)-1);
								
								@mkdir($basedir.'/'.$dirname); 
						     	@chmod($basedir.'/'.$dirname,0777);
							}
						}
						
						if($zip->extract('promotion/'))
						{
							unset($zip);
							@unlink($zipFilePath);
						}
						else 
						{
							$this->setFormError('zipfile',"Không giải nén được!");
							return ;
						}
					}
				}
			}
			else
			{
				$this->setFormError('zipfile',"File Upload phải là file nén dạng .zip !");
				return ;
			}
		}
		
		$this->setFormError('zipfile',"Bạn chưa chọn file up lên!");
		//Url::redirect_current();
	}
	
	function draw()
	{
		$this->beginForm(true);
		global $display;
		
		$display->add('msg',$this->showFormErrorMessages(1));
		
		$list	=	scandir(ROOT_PATH.'promotion/');
		
		$p_arry = array();
		
		if($list)
		{
			foreach ($list as $promo)
			{
				if($promo!='.'	&&	$promo!='..'&&is_dir(ROOT_PATH.'promotion/'.$promo))
				{
					$item['url'] 	= WEB_ROOT.'promotion/'.$promo.'/';
					$item['name'] 	= $promo;
					$item['edit'] 	= AZLib::button(Url::build_current(array('cmd'=>'edit','name'=>urlencode($promo))),'style/images/edit.gif',"Sửa tên chương trình");
					$item['delete'] = AZLib::buttonDel(Url::build_current(array('cmd'=>'delete','name'=>urlencode($promo))),'style/images/delete.gif',"Xóa chương trình");
					
					$p_arry[] = $item;
				}
			}
			
		}
		
		$display->add('p_arry',$p_arry);
			
		$display->output('AdminPromotion');
		
		$this->endForm();
	}
}

class  EditPromotionForm extends Form 
{
	function EditPromotionForm()
	{
		Form::Form('EditPromotionForm');
	}
	
	function on_submit()
	{
		$name=urldecode(Url::get('name'));
		
		$promo=urldecode(Url::get('promo'));
		if($promo && !is_dir(ROOT_PATH.'promotion/'.$promo.'/'))
		{
			rename(ROOT_PATH.'promotion/'.$name.'/',ROOT_PATH.'promotion/'.$promo.'/');
			Url::redirect_current();
		}
		else 
		{
			$this->setFormError('','Tên thư mục trống hoặc bị trùng!');
		}
	}
	
	function draw()
	{
		global $display;
		$this->beginForm();
		
		$display->add('msg',$this->showFormErrorMessages(1));
		$display->add('url',Url::build_current());
		
		$display->add('promo',Url::get('promo',Url::get('name')));
		
		$display->output('EditPromotion');
		$this->endForm();
	}
}
?>